package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.core.DispatcherServlet;
import com.webserver.entity.Article;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
@Controller
public class ArticleController {
    private static File articleDir;

    static {
        articleDir = new File("./articles");
        if (!(articleDir.exists())) {
            articleDir.mkdirs();
        }
    }

    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpServletRequest request,HttpServletResponse response){
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(title+","+author+","+content);
        File file = null;
        if (title==null||author==null||content==null){
//            file = new File("/myweb/article_fail.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/article_fail.html");
            return;
        }


        File articleFile = new File(articleDir,title+".obj");
        try(
                FileOutputStream fos = new FileOutputStream(articleFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                ){
            Article article = new Article(title,author,content);
            oos.writeObject(article);

//            file = new File(staticDir,"/myweb/article_success.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/article_success.html");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    @RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpServletRequest request,HttpServletResponse response){
        List<Article> articleList = new ArrayList<>();

        File[] data = articleDir.listFiles(f->f.getName().endsWith(".obj"));

        for (File article : data){
            try(
            FileInputStream fis = new FileInputStream(article);
            ObjectInputStream ois = new ObjectInputStream(fis);
            ){
                articleList.add((Article)ois.readObject());

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        PrintWriter pw = response.getWriter();

        pw.println("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>文章</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<center>\n" +
                "    <h1>文章阅览</h1>\n" +
                "    <table border=\"1\">");
        pw.println(
                "       <tr>\n" +
                "            <td align=\"center\">文章标题</td>\n" +
                "            <td align=\"center\">发布作者</td>\n" +
                "        </tr>");
        for (Article article : articleList){
            pw.println("<tr>\n" +
                    "            <td align=\"center\">"+article.getTitle()+"</td>\n" +
                    "            <td align=\"center\">"+article.getAuthor()+"</td>\n" +
                    "        </tr>");
        }
        pw.println("</table>\n" +
                "</center>\n" +
                "\n" +
                "</body>\n" +
                "</html>");

        response.setContentType("text/html");

    }
}
